package cn.le.crawler.zppt;

import cn.le.enums.EnumAuctionMode;
import java.util.*;

public class ZpptPropertyUtil {

    private static final String URL = "https://sf.caa123.org.cn/sf-web-ws/ws/0.1/lots";

    private static final String IMG_PREFIX = "https://sf.caa123.org.cn";
    //资产类 详情地址url
    private static final String ASSETS_ITEM_URL = "https://paimai.caa123.org.cn/pages/lots/profession.html";
    //司法类 详情地址url
    private static final String JUDICIAL_ITEM_URL = "https://sf.caa123.org.cn/lot/";

    //资产类 请求图片url
    private static final String ASSETS_REQ_IMG_URL = "https://paimai.caa123.org.cn/wt-web-ws/ws/0.1/lot/";
    //司法类 详情地址url 2611?_=1589525349196
    private static final String JUDICIAL_REQ_IMG_URL = "https://sf.caa123.org.cn/sf-web-ws/ws/0.1/lot/";


    private static final Map<Integer, String> AREA_MAP;

    static {
        AREA_MAP = new HashMap<Integer, String>() {{
            put(1, "北京市");
            put(10, "江苏省");
            put(2, "天津市");
            put(11, "浙江省");
            put(3, "河北省");
            put(12, "安徽省");
            put(4, "山西省");
            put(13, "福建省");
            put(5, "内蒙古自治区");
            put(14, "江西省");
            put(6, "辽宁省");
            put(15, "山东省");
            put(7, "吉林省");
            put(16, "河南省");
            put(8, "黑龙江省");
            put(17, "湖北省");
            put(9, "上海市");
            put(18, "湖南省");
            put(19, "广东省");
            put(20, "广西壮族治区");
            put(21, "海南省");
            put(22, "重庆市");
            put(23, "湖南省");
            put(24, "西藏自治区");
            put(25, "陕西省");
            put(26, "甘肃省");
            put(27, "青海省");
            put(28, "宁夏回族自治区");
            put(29, "新疆维吾尔族自治区");
        }};
    }

    //地区url映射
    private static Map<String, String> areaUrlMap = new HashMap<>();
    //模式url映射
    private static Map<String, EnumAuctionMode> modeUrlMap = new HashMap<>();

    private static final int PAGE_SUM = 15;

    /**
     * 获取地区
     *
     * @param pageUrl
     * @return
     */
    static String getArea(String pageUrl) {
        return areaUrlMap.get(pageUrl);
    }

    /**
     * 获取模式 司法或资产
     *
     * @param pageUrl
     * @return
     */
    static EnumAuctionMode getMode(String pageUrl) {
        return modeUrlMap.get(pageUrl);
    }

    static List<String> getPageUrls() {
        List<String> urls = new LinkedList<>();
        AREA_MAP.forEach((key, val) -> {
            for (int i = 1; i <= PAGE_SUM; i++) {
                //司法
                String url = judicial(i, val);
                urls.add(url);
                areaUrlMap.put(val, url);
                modeUrlMap.put(url, EnumAuctionMode.Judicial);
                //资产
                url = assets(i, key);
                urls.add(url);
                areaUrlMap.put(val, url);
                modeUrlMap.put(url, EnumAuctionMode.Assets);
            }
        });
        return urls;
    }

    /**
     * 司法
     *
     * @param page
     * @param province
     * @return
     */
    private static String judicial(int page, String province) {
        StringBuilder builder = new StringBuilder(URL)
                .append("?start=").append(page)
                .append("&count=12")
                .append("&sortname=&sortorder=&lotStatus=")
                .append("&province=").append(province)
                .append("&standardType=6")
                .append("&secondaryType=6");
        return builder.toString();
    }

    /**
     * 资产
     *
     * @param page
     * @param province
     * @return
     */
    private static String assets(int page, Integer province) {
        StringBuilder builder = new StringBuilder(URL)
                .append("?start=").append(page)
                .append("&count=12")
                .append("&sortname=&sortorder=&lotStatus=")
                .append("&province=").append(province)
                .append("&standardType=6")
                .append("&secondaryType=1");
        return builder.toString();
    }

    /**
     * 获取图片地址
     *
     * @param pic
     * @return
     */
    static String getImgUrl(String pic) {
        return IMG_PREFIX + pic;
    }

    /**
     * 获取详情页地址
     *
     * @param pageUrl
     * @param id
     * @param meetId
     * @return
     */
    static String getItemUrl(String pageUrl, long id, long meetId) {
        EnumAuctionMode auctionMode = modeUrlMap.get(pageUrl);
        StringBuilder url = new StringBuilder();
        if (EnumAuctionMode.Assets.equals(auctionMode)) {
            url.append(ASSETS_ITEM_URL).append("?lotId=").append(id).append("&meetId=").append(meetId);
        } else {
            url.append(JUDICIAL_ITEM_URL).append(id).append(".html");
        }
        return url.toString();
    }

    /**
     * @return
     */
    static String getReqImgUrl(long id, String pageUrl) {
        EnumAuctionMode auctionMode = modeUrlMap.get(pageUrl);

        StringBuilder builder = new StringBuilder();
        if (EnumAuctionMode.Assets.equals(auctionMode)) {
            //https://paimai.caa123.org.cn/wt-web-ws/ws/0.1/lot/238669?time=1589525275219&_=1589525274843
            builder.append(ASSETS_REQ_IMG_URL).append(id).append("?time=").append(new Date().getTime());
        } else {
            //https://sf.caa123.org.cn/sf-web-ws/ws/0.1/lot/2611?_=1589525349196
            builder.append(JUDICIAL_REQ_IMG_URL).append(id).append("?_").append(new Date().getTime());
        }
        return builder.toString();
    }
}